package com.lxw.manage.course.controller;

import com.lxw.api.courese.CourseControllerApi;
import com.lxw.client.CmsPageClient;
import com.lxw.framework.domain.course.*;
import com.lxw.framework.domain.course.ext.TeachplanNode;
import com.lxw.framework.domain.course.response.CoursePublishResult;
import com.lxw.framework.domain.course.response.CourseView;
import com.lxw.framework.exception.CustomException;
import com.lxw.framework.model.response.CommonCode;
import com.lxw.framework.model.response.QueryResponseResult;
import com.lxw.framework.model.response.QueryResult;
import com.lxw.framework.model.response.ResponseResult;
import com.lxw.manage.course.dao.TeachPlanMediaRepository;
import com.lxw.manage.course.dao.TeachplanMediaPubRepository;
import com.lxw.manage.course.service.ICourseMarketService;
import com.lxw.manage.course.service.ICourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class CourseController implements CourseControllerApi {
    @Autowired
    ICourseService courseService;
    @Autowired
    ICourseMarketService courseMarketService;

    @Override
    @GetMapping("teachplan/list/{courseId}")
    public TeachplanNode findTeachplanList(@PathVariable String courseId) {

        return courseService.findTeachplanList(courseId);
    }

    @Override
    @GetMapping("teachplan/get/{id}")
    public Teachplan findTeachplanById(@PathVariable String id) {

        return courseService.findTeachplanById(id);
    }

    @Override
    @PostMapping("teachplan/add")
    public ResponseResult addTeachplan(@RequestBody Teachplan teachplan) {

        return courseService.addTeachplan(teachplan);
    }

    @Override
    @DeleteMapping("teachplan/del/{id}")
    public ResponseResult delTeachplan(@PathVariable String id) {
        return courseService.delTeachplan(id);
    }

    @Override
    @PostMapping("coursebase/add")
    public ResponseResult addCourse(@RequestBody CourseBase courseBase) {
        //非空校验
        if(StringUtils.isEmpty(courseBase.getName())){
            throw new CustomException(CommonCode.FAIL);
        }

        courseBase.setStatus("202001");
        return courseService.addCourse(courseBase);
    }

    @Override
    @GetMapping("coursebase/list/{page}/{pageSize}")
    public QueryResponseResult findList(@PathVariable Integer page, @PathVariable Integer pageSize) {
        Page<CourseBase> list = courseService.findList(page, pageSize);
        QueryResult<CourseBase> queryResult = new QueryResult<>();
        queryResult.setTotal(list.getTotalElements());
        queryResult.setList(list.getContent());
        return new QueryResponseResult(CommonCode.SUCCESS,queryResult);
    }

    @Override
    @GetMapping("coursebase/get/{courseId}")
    public CourseBase findCoursebaseByID(@PathVariable String courseId) {

        return courseService.findCoursebaseById(courseId);
    }

    @Override
    @PostMapping("coursebase/edit/{courseId}")
    public ResponseResult editCourse(@PathVariable String courseId,@RequestBody CourseBase courseBase) {
       courseService.editCourse(courseId, courseBase);

        return new ResponseResult(CommonCode.SUCCESS);
    }

    @Override
    @GetMapping("courseMaket/get/{courseId}")
    public CourseMarket findCourseMarketById(@PathVariable String courseId) {


        return courseMarketService.findCourseMarketById(courseId);
    }

    @Override
    @PostMapping("courseMaket/update/{courseId}")
    public ResponseResult editCourseMarket(@PathVariable String courseId,@RequestBody CourseMarket courseMarket) {
        /**
         * 先查询课程信息是否存在
         */
        CourseBase coursebaseById = courseService.findCoursebaseById(courseId);//
        if(coursebaseById==null){
            throw new CustomException(CommonCode.FAIL);
        }
        //非空 其他业务判断
        courseMarket.setId(courseId);
        return courseMarketService.update(courseMarket);
    }

    @Override
    @PostMapping("coursepic/add")
    public ResponseResult addCoursePic(String courseId, String pic) {
        //非空校验TODO
        return courseService.addCoursePic(courseId,pic);
    }

    @Override
    @GetMapping("coursepic/list/{courseId}")
    public CoursePic findCoursePicById(@PathVariable String courseId) {


        return courseService.findCoursePicById(courseId);
    }

    @Override
    @DeleteMapping("coursepic/delete")
    public ResponseResult delCoursePicById(@RequestParam String courseId) {
        return courseService.delCoursePicById(courseId);
    }

    @Override
    @GetMapping("/getModelData/{courseId}")
    public CourseView getModelData(@PathVariable String courseId) {
        return courseService.getModelData(courseId);
    }

    @Override
    @GetMapping("/preview/{id}")
    public CoursePublishResult preview(@PathVariable String id) {


        return courseService.preview(id);
    }

    @Override
    @GetMapping("/publish/{id}")
    public CoursePublishResult postPage(@PathVariable String id) {
        return courseService.publish(id);
    }

    @Override
    @PostMapping("savemedia")
    public ResponseResult savemedia(@RequestBody TeachplanMedia teachplanMedia) {
        return courseService.savemedia(teachplanMedia);
    }

    @Autowired
    CmsPageClient cmsPageClient;

    @GetMapping("getpage")
    public Object getPage(String id){
        return cmsPageClient.findById(id);
    }



}
